<?php 
namespace Home\Controller;
use Think\Controller;

class CompanyController extends Controller {
	
	//发布职位
	public $Job;
	public $Com;
	public $Rz;
	public $Cr;
 	public $Prod;
 	public $com_id;
	public $Jl;
	public $Wx;
	public $Pexp;
	public $Edu;
	public $Opus;  
	public function __construct(){
		parent::__construct();
		$this->Jobs = D('Jobs'); 
		$this->Com 	= D('Companys');
		$this->Rz 	= D('Com_rz');
		$this->Cr 	= D('Com_creater');
		$this->Prod = D('Com_prod');
		$this->Jl 	= D('Postresumes');
		$this->Wx 	= D('Workexp');
		$this->Pexp = D('Proexp');
		$this->Edu  = D('Eduexp');    
		$this->Opus = D('Opus');
			   $uid = cookie('sn')?cookie('sn'):0;
		$this->com_id = D('Companys')->field('com_id')->where(['uid'=>$uid])->find()['com_id'];


	}

	public function create(){
		//权限检测:检测登陆并检测COOKIE是否正确
		if(cookie('Qkey')){
			if(!homechcekck()){
				$this->error('信息不正确,请重新登陆!','/',1);
				exit;
			}
		}else{
			$this->error('请登陆!','/Home/User/login',1);
		}
		//检测账号类型:Utype = 1为企业,0为求职者
		if(cookie('Utype') != 1 ){
			$this->error('您没有权限查看!','/',1);
		}
		//检测账号状态:Usta = 2为通过审核,0为未通过审核
		if(cookie('Utype') != 1 ){
			$this->error('您没有权限查看!','/',1);
		}

		if (empty($_POST)) {
			$cat = D('Admin/cate')->getTree();
			$this->assign('cat',$cat);
			$this->display('Company/create');
		}else{
			if(!$check = D('Admin/cate')->where(['cat_name'=>I('post.positionType')])->find()){
				exit('错误');
			}
			//往数据库插入数据
			$this->Jobs->com_id = $this->com_id;
			$this->Jobs->positionType 			=I('post.positionType');
			$this->Jobs->positionName 			=I('post.positionName');
			$this->Jobs->department 			=I('post.department');
			$this->Jobs->jobNature 				=I('post.jobNature');
			$this->Jobs->salaryMin 				=I('post.salaryMin');
			$this->Jobs->salaryMax 				=I('post.salaryMax');
			$this->Jobs->workYear 				=I('post.workYear');
			$this->Jobs->education 				=I('post.education');
			$this->Jobs->positionAdvantage 		=I('post.positionAdvantage');
			$this->Jobs->positionAddress 		=I('post.positionAddress');
			$this->Jobs->positionLng 			=I('post.positionLng');
			$this->Jobs->email 					=I('post.email');
			$this->Jobs->forwardEmail 			=I('post.forwardEmail');
			if ($id = $this->Jobs->add()) {//判断insert操作是成功
				cookie('pstatus',1);		//设置将要跳转页面的进入凭据
				echo json_encode(['success'=>'1','content'=>U('/Home/company/postresumeSucc/id/'.$id)]);//返回json给前台
			}else{
				echo json_encode(['msg'=>'出错啦']);
			}
		}
		
	}
	public function postresumeSucc(){
		//权限检测:检测登陆并检测COOKIE是否正确
		if(cookie('Qkey')){
			if(!homechcekck()){
				$this->error('信息不正确,请重新登陆!','/');
				exit;
			}
		}else{
			//未登陆状态动作待写
		}
		//检测账号类型:Utype = 1为企业,0为求职者
		if(cookie('Utype') != 1 ){
			$this->error('您没有权限查看!','/');
		}
		//检测账号状态:Usta = 2为通过审核,0为未通过审核
		if(cookie('Usta') != 1){
			$this->error('请填写公司信息并等待审核','/Home/Company/cominfo1');
		}
		if (cookie('pstatus')!=1) {
			$this->redirect('/');
		}
		cookie('pstatus',null);
		$this->display('/Company/postresumeSucc');
	}

	public function cominfo1(){
		if(cookie('Qkey')){
			if(!homechcekck()){
				$this->error('信息不正确,请重新登陆!','/',1);
				exit;
			}
		}else{
			$this->error('请登陆!','/Home/User/login',1);
		}
		//检测账号类型:Utype = 1为企业,0为求职者
		if(cookie('Utype') != 1 ){
			$this->error('您没有权限查看!','/',1);
		}
		//检测账号状态:Usta = 2为通过审核,0为未通过审核
		if(cookie('Utype') != 1 ){
			$this->error('您没有权限查看!','/',1);
		}

		if (empty($_POST)) {
			$cat = D('Admin/cate')->getTree(); //获取分类
			$this->assign('cat',$cat);			//赋值给模板
			$this->display('Company/create');
		}
		if (!IS_POST) {
			if ($this->com_id) {  	
				$this->error('你已经创建公司了,试试发布职位把','/Home/company/create');
			}
		$this->display('/company/index01');
		}else{

			$this->Com->uid 		= cookie('sn');
			$this->Com->comname 	= I('post.companyName');
			$this->Com->com_sname 	= I('post.companyShortName');
			$this->Com->com_logo 	= I('post.logo')?I('post.logo'):'暂时没有图片';
			$this->Com->com_url 	= I('post.companyUrl');
			$this->Com->com_city 	= I('post.city');
			$this->Com->com_ly 	 	= I('post.industryField');//公司領域
			$this->Com->com_size 	= I('post.companySize');//
			$this->Com->com_sta 	= I('post.financeStage');//融資狀態
			$this->Com->com_sintro 	= I('post.companyFeatures');//一句話介紹
			

			if ($comid = $this->Com->add()) {
					$a = json_decode($_POST['stages'],true);
					foreach ($a as $key => $v) {
						if (empty($v['stage']) ||  empty($v['org'])) {
							continue;
						}
						$this->Rz->com_id 	= $comid;
						$this->Rz->stage 	= $v['stage'];
						$this->Rz->org 		= $v['org'];
						$this->Rz->add();
			}
			echo json_encode(['success'=>'1','content'=>U('/Home/company/cominfo2/id/'.$comid)]);
			}else{
				echo json_encode(['msg'=>'错误啦']);
			}


		}

	}
		

	public function cominfo2(){
		if(cookie('Qkey')){
			if(!homechcekck()){
				$this->error('信息不正确,请重新登陆!','/',1);
				exit;
			}
		}else{
			$this->error('请登陆!','/Home/User/login',1);
		}
		//检测账号类型:Utype = 1为企业,0为求职者
		if(cookie('Utype') != 1 ){
			$this->error('您没有权限查看!','/',1);
		}
		//检测账号状态:Usta = 2为通过审核,0为未通过审核
		if(cookie('Utype') != 1 ){
			$this->error('您没有权限查看!','/',1);
		}

		if (empty($_POST)) {
			$cat = D('Admin/cate')->getTree();
			$this->assign('cat',$cat);
			$this->display('Company/create');
		}
		if(!IS_POST){
			$this->display('/company/tag');
				if (!$this->com_id) {
				$this->error('你还没有填写公司信息','/Home/company/cominfo1');
			}
		}else{

			$comid = $this->com_id;
    		$this->Com->find($comid);
			$this->Com->com_tag = I('post.labels');
			if($this->Com->save()){
				echo json_encode(['url'=>U('/Home/company/cominfo3/id/'.$comid)]);
			}else{
				echo json_encode(['msg'=>'出错啦']);
			}
		}
		
	}
	public function cominfo3(){
		if(cookie('Qkey')){
			if(!homechcekck()){
				$this->error('信息不正确,请重新登陆!','/',1);
				exit;
			}
		}else{
			$this->error('请登陆!','/Home/User/login',1);
		}
		//检测账号类型:Utype = 1为企业,0为求职者
		if(cookie('Utype') != 1 ){
			$this->error('您没有权限查看!','/',1);
		}
		//检测账号状态:Usta = 2为通过审核,0为未通过审核
		if(cookie('Utype') != 1 ){
			$this->error('您没有权限查看!','/',1);
		}

		if (empty($_POST)) {
			$cat = D('Admin/cate')->getTree();
			$this->assign('cat',$cat);
			$this->display('Company/create');
		}
		if (!IS_POST) {
			$this->display('/company/founder');
			exit;
		}else{
			 // print_r($_POST);exit;
			$w = I('post.w');
			$h = I('post.h');
			$upload = new \Think\Upload();
			$upload->exts = array('jpg','jpeg','png');
			$upload->rootPath = APP_PATH.'Public/';
			$upload->savePath = 'Home/com_imgs/';
			if(!$info=$upload->upload()){
				exit('error');
			}else{
				$rp 	= '/Public/'.$info['myfiles']['savepath'].$info['myfiles']['savename'];
				$img 	= new \Think\Image();
			    $img->open(APP_PATH.$rp);
			    $thumb 	= '/Public/'.$info['myfiles']['savepath'].'thumb-'.$info['myfiles']['savename'];
				$a 		= $img->thumb($w,$h)->save(APP_PATH.$thumb);
				echo $thumb;
				exit;
			}

		}
		
	}
	public function cominfo3save(){
		if(cookie('Qkey')){
			if(!homechcekck()){
				$this->error('信息不正确,请重新登陆!','/',1);
				exit;
			}
		}else{
			$this->error('请登陆!','/Home/User/login',1);
		}
		//检测账号类型:Utype = 1为企业,0为求职者
		if(cookie('Utype') != 1 ){
			$this->error('您没有权限查看!','/',1); 
		}
		//检测账号状态:Usta = 2为通过审核,0为未通过审核
		if(cookie('Utype') != 1 ){
			$this->error('您没有权限查看!','/',1);
		}

		if (empty($_POST)) {
			$cat = D('Admin/cate')->getTree();
			$this->assign('cat',$cat);
			$this->display('Company/create');
		}
		if (!IS_POST) {
			exit('error');
		}else{
			if (!$this->com_id) {
				$this->error('你还没有填写公司信息','/Home/company/cominfo1');
			}
		 	$com_id  	= $this->com_id;
			$list 	 	= I('post.leaderInfos');
			foreach ($list as $v) {
				$this->Cr->com_id 		= $com_id;
				$this->Cr->cre_name 	= $v['name'];
				$this->Cr->cre_position	= $v['position'];
				$this->Cr->cre_weibo 	= $v['weibo'];
				$this->Cr->cre_intro 	= $v['remark'];
				$this->Cr->cre_img 		= $v['photo'];
				$this->Cr->add();

			}
			$this->redirect("/Home/company/cominfo4/id/$com_id");
		}
	}
	public function cominfo4(){
		if(cookie('Qkey')){
			if(!homechcekck()){
				$this->error('信息不正确,请重新登陆!','/',1);
				exit;
			}
		}else{
			$this->error('请登陆!','/Home/User/login',1);
		}
		//检测账号类型:Utype = 1为企业,0为求职者
		if(cookie('Utype') != 1 ){
			$this->error('您没有权限查看!','/',1);
		}
		//检测账号状态:Usta = 2为通过审核,0为未通过审核
		if(cookie('Utype') != 1 ){
			$this->error('您没有权限查看!','/',1);
		}

		if (empty($_POST)) {
			$cat = D('Admin/cate')->getTree();
			$this->assign('cat',$cat);
			$this->display('Company/create');
		}
		if (!IS_POST) {
		$this->display('/company/index02');
		}else{
			if (!$this->com_id) {
				$this->error('你还没有填写公司信息','/Home/company/cominfo1');
			}
			$com_id  = $this->com_id;
			$prolist = I('post.productInfos');
			foreach ($prolist as $v) {
				$this->Prod->com_id 	= $com_id;
				$this->Prod->prod_url 	= $v['productUrl'];
				$this->Prod->prod_name 	= $v['product'];
				$this->Prod->prod_intro = $v['productProfile'];
				$this->Prod->prod_img 	= $v['productPicUrl'];
				$this->Prod->add();
			}
			$this->redirect("/Home/company/cominfo5/id/$com_id");
		}
		
	}
	public function cominfo5(){
		if(cookie('Qkey')){
			if(!homechcekck()){
				$this->error('信息不正确,请重新登陆!','/',1);
				exit;
			}
		}else{
			$this->error('请登陆!','/Home/User/login',1);
		}
		//检测账号类型:Utype = 1为企业,0为求职者
		if(cookie('Utype') != 1 ){
			$this->error('您没有权限查看!','/',1);
		}
		//检测账号状态:Usta = 2为通过审核,0为未通过审核
		if(cookie('Utype') != 1 ){
			$this->error('您没有权限查看!','/',1);
		}

		if (empty($_POST)) {
			$cat = D('Admin/cate')->getTree();
			$this->assign('cat',$cat);
			$this->display('Company/create');
		}
		if (!IS_POST) {
			$this->display('/company/index03');
		}else{
			if (!$this->com_id) {
				$this->error('你还没有填写公司信息','/Home/company/cominfo1');
			}
			$com_id  = $this->com_id;
			$this->Com->find($com_id);
			$this->Com->com_wenhua = I('post.companyProfile');
			if($this->Com->save()){
				$this->redirect("/Home/company/myhome/");
			}
		}
		
	}
	public function myhome(){
		if(cookie('Qkey')){
			if(!homechcekck()){
				$this->error('信息不正确,请重新登陆!','/',1);
				exit;
			}
		}else{
			$this->error('请登陆!','/Home/User/login',1);
		}
		//检测账号类型:Utype = 1为企业,0为求职者
		if(cookie('Utype') != 1 ){
			$this->error('您没有权限查看!','/',1);
		}
		//检测账号状态:Usta = 2为通过审核,0为未通过审核
		if(cookie('Utype') != 1 ){
			$this->error('您没有权限查看!','/',1);
		}


		if (!$this->com_id) {
			$this->error('你还没有注填写公司信息呢','/Home/company/cominfo1',3);
			exit;
		}
		if (cookie('Utype')==0) {
			$this->error('去发布简历吧','/Home/jianli/jianli',3);
		}

		$com 	= $this->Com->find($this->com_id);
 		$a  	=  spliti( "," ,$com['com_tag'] ,10);
		$prod 	= $this->Prod->where(['com_id'=>$this->com_id])->select();
		$create = $this->Cr->where(['com_id'=>$this->com_id])->select();
		$this->assign('tag',$a);
		$this->assign('com',$com);
		$this->assign('prod',$prod);
		$this->assign('create',$create);
		$this->display('/company/myhome');
	}

	public function position(){
		$cnt = $this->Jobs->where(['com_id'=>$this->com_id])->count('*');
		$pager = new \Think\Page($cnt,5);
		$job = $this->Jobs->where(['com_id'=>$this->com_id])->limit($pager->firstRow,$pager->listRows)->select();
		$this->assign('page',$pager->show());
		$this->assign('job',$job);
		$this->display('Company/positions');
	}

	public function daiding(){
		$res = D('Resumes_post')->where(['com_id'=>$this->com_id,'status'=>'0'])->select();
		foreach ($res as $k => $v) {
			$v['jl'] = D('Postresumes')->where(['pid'=>$v['pid']])->select();
			$res[$k]=$v;
		}

		$this->assign('res',$res);
		$this->display('Company/canInterviewResumes');
	}	

	public function pass(){
		$res = D('Resumes_post')->where(['com_id'=>$this->com_id,'status'=>'2'])->select();
		// print_r($res);exit;
		foreach ($res as $k => $v) {
			$v['jl'] = D('Postresumes')->where(['pid'=>$v['pid']])->select();
			$res[$k]=$v;
		}

		$this->assign('res',$res);
		$this->display('Company/canInterviewResumes');
	}	

	public function fail(){
		$res = D('Resumes_post')->where(['com_id'=>$this->com_id,'status'=>'1'])->select();
		// print_r($res);exit;
		foreach ($res as $k => $v) {
			$v['jl'] = D('Postresumes')->where(['pid'=>$v['pid']])->select();
			$res[$k]=$v;
		}

		$this->assign('res',$res);
		$this->display('Company/canInterviewResumes');
	}


	public function passmodel(){
		$ps = D('Resumes_post');
		$id = I('post.id');
		$type = I('post.type');
		if (!$res = $ps->find($id)) {
			echo json_encode(['msg'=>'错误']);
			exit;
		}elseif($res['status']==2){
			echo json_encode(['msg'=>'已经申请通过']);
			exit;
		}elseif ($res['status']==1) {
			echo json_encode(['msg'=>'你已经拒绝申请了']);
			exit;
		}
		switch ($type) {
			case '2':
			$ps->status = 2;
			if($ps->save()){
				echo json_encode(['msg'=>'通过:操作成功']);
				exit;
			}
			break;
			case '1':

			$ps->status=1;
			if($ps->save()){
				echo json_encode(['msg'=>'拒绝:操作成功']);
				exit;
			}


			default:
				# code...
				break;
		}

	}

	public function prev(){
			//权限检测:检测登陆并检测COOKIE是否正确
			if(cookie('Qkey')){
				if(!homechcekck()){
					$this->error('信息不正确,请重新登陆!','/',1);
					exit;
				}
			}else{
				$this->error('请登陆!','/Home/User/login',1);
			}

			//检测账号类型:Utype = 1为企业,0为求职者
			// if (cookie('Utype') != 0 ) {
			// 	$this->error('你是企业用户~去发布职位吧','/Home/company/create',1);
			// }

			
		// public $Jl;
		// public $Wx;
		// public $Pexp;
		// public $Edu;
		// public $Opus;
			$base 		= $this->Jl->find(I('get.id'));
			$workexp 	= $this->Wx->where(['pid'=>$base['pid']])->select();
			$pexp 		= $this->Pexp->where(['pid'=>$base['pid']])->select();
			$edu 		= $this->Edu->where(['pid'=>$base['pid']])->select();
			$opus 		= $this->Opus->where(['pid'=>$base['pid']])->select();
			$this->assign('base',$base);
			$this->assign('workexp',$workexp);
			$this->assign('pexp',$pexp);
			$this->assign('edu',$edu);
			$this->assign('opus',$opus);
			$this->display('user/preview');
		}

	public function haveNoticeResumes(){
		$this->error('页面建设中...','javascript:history.back(-1);',1);
	}

}